function [ output_args ] = detectForeground( path, filename, frames, initframes )
%DETECTFOREGROUND Summary of this function goes here
%   Detailed explanation goes here
    DIFF_THR = 0.1;
    ALPHA = 0.1;

    % read video file structure and first frame
    %m = mmreader(file);
    
    test = imread(sprintf('%s%s_%04d.jpeg',path,filename,1));        
    
    init = zeros(size(test,1), size(test,2), size(test,3), initframes);
    %img_mean = zeros(size(test,1), size(test,2), size(test,3));
    
    % initialization
    fprintf('start initialization \n');
    for n=1:initframes
        %img = read(m,n);
        imgname = sprintf('%s%s_%04d.jpeg',path,filename,n);
        img = im2double(imread(imgname));        
        img_color = lowLevelImgProcColor(img);
        %img_mean = imadd(img_mean, img_color);
        init(:,:,:,n) = img_color(:,:,:);
        
        %img_gray = lowLevelImgProc(img);
        %background = img_gray;
    end  
    %img_mean = imdivide(img_mean, initframes);
    img_mean = mean(init,4);
    
    img_var = zeros(size(img_mean,1), size(img_mean,2), size(img_mean,3));
    
    for n=1:initframes
        img_var = imadd(img_var(:,:,:), imsubtract(init(:,:,:,n), img_mean(:,:,:)).^2);
    end
    img_std = imdivide(img_var, initframes).^(1.0/2.0);
    
    %figure(2)
    %imagesc(img_mean);
    %figure(3)
    %imagesc(img_var);
    
    fprintf('img size %f %f %f  %f\n', size(init,1), size(init,2), size(init,3), size(init,4));
    fprintf('mean size %f %f %f \n', size(img_mean,1), size(img_mean,2), size(img_mean,3),size(img_mean,4) );
    fprintf('std size %f %f %f \n', size(img_std,1), size(img_std,2), size(img_std,3),size(img_std,4) );
    
    %imshow(img_mean,[]);
    
    fprintf('initialization stopped \n');
    
    %lastframe = background;
    % detection
    fprintf('start detection \n');
    for n=initframes:frames
        %img = read(m,n);
        imgname = sprintf('%s%s_%04d.jpeg',path,filename,n);
        img = im2double(imread(imgname));
        %img_gray= lowLevelImgProc(img);
        img_color = lowLevelImgProcColor(img);
        
        %diff = imabsdiff(img, background);  %Background differencing
        %diff = imabsdiff(img_gray,lastframe);     %Framedifferencing 
        diff = imabsdiff(img_color,img_mean);     %Framedifferencing 
        
        %Threshold, which segments foreground.
        %bw = im2bw(diff,DIFF_THR); 
        %bw = im2bw(diff,graythresh(diff));
        bw = zeros(size(diff,1), size(diff,2));
        
        for i = 1 : size(diff,1)
            for j = 1 : size(diff,2)
                if((diff(i,j,1) > (img_std(i,j,1)*3.5)) || (diff(i,j,2) > (img_std(i,j,2)*3.5)) || (diff(i,j,3) > (img_std(i,j,3)*3.5)))
                    bw(i,j) = 1.0;
                end
            end
        end
        
        bw = imopen(bw, strel('disk',3));  
        
        img_mean = imadd( immultiply(img_mean,((1.0 - ALPHA))), immultiply(img_color,ALPHA));
        img_std = imadd(immultiply(img_std,(1.0 - ALPHA)), immultiply(diff,ALPHA));
        
        hold on;
        figure(1);
        imagesc(bw);
        colormap('gray');
        hold off;
        
        figure(4);
        imagesc(img_color);
        hold off;
        
        %lastframe = img_gray;
    end
    fprintf('detection ended \n');
    
    pause off;
end

